<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增二级' : '查看二级'"
    :close-on-click-modal="false"
    :modal="false"
    :visible.sync="visibles"
    width="30%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @submit.native.prevent="dataFormSubmit()" label-width="80px">
    <el-form-item label="分类名称" prop="className">
      <el-input v-model="dataForm.className" placeholder="分类名称"></el-input>
    </el-form-item>
    <el-form-item label="显示顺序" prop="sequence">
      <el-input type="number" style="width: 200px" v-model="dataForm.sequence" placeholder="显示顺序"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visibles = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visibles: false,
        dataForm: {
          id: 0,
          sequence: '',
          className: '',
          parentId: 0
        },
        dataRule: {
          className: [
            { required: true, message: '分类名称不能为空', trigger: 'blur' }
          ],
          sequence: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, parentId) {
        if (id != undefined) {
          this.dataForm.id = id
        } else {
            this.dataForm.id = 0
        }
        this.dataForm.parentId = parentId
        this.visibles = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/activity/goods/getPurchaseLargeClassById/${this.dataForm.id}`),
              method: 'post',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.className = data.data.className
                this.dataForm.sequence = data.data.sequence
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/activity/goods/${!this.dataForm.id ? 'addPurchaseLargeClass' : 'modifyPurchaseLargeClass'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'className': this.dataForm.className,
                'sequence': this.dataForm.sequence,
                'level': 1,
                'parentId': this.dataForm.parentId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visibles = false
                    this.$emit('refreshDataList', this.dataForm.parentId)
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
